package com.LeetCode.Array;

import org.junit.Test;

/**
 * 螺旋矩阵2
 */
public class generateMatrix {

    public int[][] generateMatrix(int n, int m) {
        if(n == 0) return new int[0][0];
        int start = 1, end = n * m;
        int[][] ans = new int[n][m];
        int l = 0, r = m-1, t = 0, b = n-1;
        while(start <= end){
            for(int i = l; i<=r; i++) ans[t][i] = start++;
            t++;
            for(int i = t; i<=b; i++) ans[i][r] = start++;
            r--;
            for(int i = r; i>=l; i--) ans[b][i] = start++;
            b--;
            for(int i = b; i>=t; i--) ans[i][l] = start++;
            l++;
        }
        return ans;
    }

    @Test
    public void test(){
        for(int[] arr : generateMatrix(3,4)){
            for(int a : arr){
                System.out.print(a + " ");
            }
            System.out.println();
        }
    }
}
